package com.tju320.elm.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tju320.elm.po.Admin;
//建立，关闭连接数据库的工具类
public class DBUtil {
	private static final String DRIVER="com.mysql.jdbc.Driver";
	private static final String USER ="root";
	private static final String PASSWORD="980312";
	private static final String URL="jdbc:mysql://localhost:3306/elm?characterEncoding=UTF-8";
	public static Connection getConnection() {
		Connection conn=null;
        try {
        	// 1.连接驱动类
			Class.forName(DRIVER);
			// 2.获取连接
			conn = DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	//关闭资源
		public static void close(ResultSet rs,PreparedStatement pst,Connection conn) {
			//关闭ResultSet的对象，如果本身是null就不关了
			if(rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				rs=null;
			}
			//关闭PreparedStatement的对象，如果本身是null就不关了
			if(pst!=null) {
				try {
					pst.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pst=null;
			}
			//关闭Connection的对象，如果本身是null就不关了
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
					e.printStackTrace();
				}
				conn=null;
			}
			
		}
		
	

}
